<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input v-model="queryForm.name" placeholder="请输入用户昵称..." clearable />
      </el-col>
      <el-button type="primary" @click="initUserList">搜索</el-button>
    </el-row>
    <el-table :data="tableData" stripe style="width: 100%;text-align: center">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="studentName" label="用户昵称" width="200" />
      <el-table-column prop="studentId" label="学号" />
      <el-table-column prop="clockTime" label="打卡日期" />
      <el-table-column label="打卡状态">
        <template v-slot="scope">
          <el-tag v-if="scope.row.clockStatus == 0" type="warning">未打卡</el-tag>
          <el-tag v-if="scope.row.clockStatus == 1" type="success">已打卡</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template v-slot="scope">
          <el-button type="danger" size="small" icon="el-icon-delete" @click="deleted(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="queryForm.pageNum"
      :page-sizes="[10, 20, 30, 40,50]"
      :page-size="queryForm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

  </el-card>
</template>

<script>
import { getStudentClockListPage, deleteStudentClock } from '@/api/table'
export default {
  data() {
    return {
      queryForm: {
        pageSize: 10,
        pageNum: 1,
        name: ''
      },
      tableData: [],
      total: 0

    }
  },
  created() {
    this.initUserList()
  },
  computed: {
    isAdmin() {
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      return userInfo && userInfo.studentId === 'admin' && userInfo.identity === '1'
    },
    isTeacher() {
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      return userInfo && userInfo.identity == 1 && userInfo.studentId !== 'admin'
    },
    isStudent() {
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      return userInfo && userInfo.identity == 0
    },
    currentUser() {
      return JSON.parse(localStorage.getItem('userInfo')) || {}
    }
  },
  methods: {
    initUserList() {
      const userInfo = this.currentUser
      if (this.isAdmin) {
        // 管理员查所有签到
        getStudentClockListPage(this.queryForm).then(res => {
          if (res.code == 200) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      } else if (this.isTeacher) {
        // 老师查自己和自己班级学生的签到
        getStudentClockListPage({ ...this.queryForm, teacherId: userInfo.id }).then(res => {
          if (res.code == 200) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      } else if (this.isStudent) {
        // 学生查自己的签到
        getStudentClockListPage({ ...this.queryForm, userId: userInfo.id }).then(res => {
          if (res.code == 200) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      }
    },
    handleSizeChange(e) {
      console.log(e)
      this.queryForm.pageSize = e
      this.initUserList()
    },
    handleCurrentChange(e) {
      this.queryForm.pageNum = e
      this.initUserList()
    },
    deleted(item) {
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteStudentClock({ id: item.clockId }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.initUserList()
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
